<template>
  <div class="common-layout">
    <el-container style="height: 100%">
      <el-container style="height: 100%">
        <!--侧边栏暂时搁置-->
        <el-aside width="210px" class="aside" v-if="true">
          <div>
            <div class="aside-button">十 新建对话</div>
          </div>
          <div class="aside-history">
            <el-input
              clearable
              prefix-icon="el-icon-search"
              placeholder="搜索历史对话"
              v-model="historyChatKey"
              class="keyinput"
            ></el-input>
            <div class="icon"><img src="@/assets/magnifier.png" alt="" /></div>
          </div>
          <div class="aside-today-history">
            <p class="tips">最近对话</p>
          </div>
          <div class="aside-two-days-history">
            <div class="itembox">
              <p class="tips">工具箱</p>
              <div class="items">全部工具</div>
            </div>
            <div class="itembox">
              <p class="tips">常用</p>
              <div class="items">公文写作</div>
            </div>
          </div>
        </el-aside>
        <el-container>
          <el-main class="main">
            <div class="main-content">
              <el-card
                class="main-content-card-welcome"
                v-if="aiHistoryList.length === 0 && userHistoryList.length === 0"
              >
                👏🏻请👏输👏入🎉问🎉题🎉
              </el-card>
              <el-card class="main-content-history" v-else>
                <div v-for="(item, index) in userHistoryList" :key="index">
                  <div class="main-content-history-user">
                    <span>{{ item.content }}</span>
                    <el-avatar
                      src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                    />
                  </div>
                  <div class="main-content-history-ai" v-if="aiHistoryList[index]">
                    <el-avatar
                      src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                    />
                    <span>{{ aiHistoryList[index].content }}</span>
                  </div>
                </div>
              </el-card>
            </div>

            <el-footer class="footer">
              <el-input
                v-model="textValue"
                class="footer-input"
                type="text"
                placeholder="请输入问题"
              >
              </el-input>
              <el-button
                :type="textValue ? 'primary' : 'info'"
                class="footer-button"
                :icon="Promotion"
                @click="sendMessage"
                :disabled="!textValue"
              ></el-button>
            </el-footer>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { tongYiApi } from '@/api'
import { ref } from 'vue'
// Promotion 图标
import { Promotion } from '@element-plus/icons-vue'

// 要展示数据的数组，数组里面是数据对象
interface historyListItem {
  content: string
}
let historyChatKey = ref<String>('')
const userHistoryList = ref<historyListItem[]>([])
const aiHistoryList = ref<historyListItem[]>([])
// 输入的文本内容
const textValue = ref<string>('')
let messages = ref<[]>([])
const askTongYiApi = async () => {
  const params = {
    model: 'qwen-turbo',
    input: {
      messages: [
        {
          role: 'system',
          content: 'You are a helpful assistant.'
        },
        {
          role: 'user',
          content: textValue.value
        }
      ]
    },
    parameters: {
      result_format: 'message'
    }
  }

  // 发送请求
  const res = await tongYiApi(params)
  return res
}

// 发送消息
const sendMessage = async () => {
  if (textValue.value === '') return
  messages.value.push({ text: textValue.value , user: true })
  const userMessage = textValue.value 
  textValue.value = ''

  try {
    const response = await fetch('http://117.50.181.115/app/a28fbd5e-8fa4-4937-8057-c2b1c1b38677/develop', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ message: userMessage })
    })

    if (!response.body) {
      throw new Error('ReadableStream not supported!')
    }

    const reader = response.body.getReader()
    const decoder = new TextDecoder('utf-8')
    let done = false

    while (!done) {
      const { value, done: doneReading } = await reader.read()
      done = doneReading
      const chunkValue = decoder.decode(value, { stream: true })
      messages.value.push({ text: chunkValue, user: false })
    }
  } catch (error) {
    console.error('Error:', error)
    messages.value.push({ text: 'Error communicating with the bot.', user: false })
  }
}
</script>

<style scoped lang="scss">
.common-layout {
  height: 100%;
}

.header,
.main {
  background: #f7f8fc;
}

.header {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 20px;
}

.main {
  height: calc(100% - 20px);
  border-radius: 32px;
  margin: 0 20px;

  .main-content {
    width: 100%;
    height: calc(100% - 60px);
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .main-content-card-welcome {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 20%;
    border-radius: 16px;
  }

  .main-content-history {
    width: 100%;

    .main-content-history-user {
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
    .main-content-history-ai {
      display: flex;
      align-items: center;
    }
  }
}

.aside {
  padding: 21px 12px;
  display: flex;
  flex-direction: column;
  padding-top: 21px;
  justify-content: flex-start;
  background: linear-gradient(180deg, #e3e8ff 0%, #dfeaff 34%, #f5fcff 100%);
  align-items: center;
  .aside-button {
    margin-bottom: 12px;
    cursor: pointer;
    width: 186px;
    height: 33px;
    border-radius: 5px 5px 5px 5px;
    border: 1px solid;
    font-family:
      PingFang SC,
      PingFang SC;
    padding-top: 10px;
    padding-left: 10px;
    font-weight: 600;
    font-size: 12px;
    color: #0076f4;
    line-height: 12px;
    text-align: left;
    font-style: normal;
    text-transform: none;
  }
  .aside-history {
    margin-bottom: 20px;
    position: relative;
    .icon {
      position: absolute;
      top: 6px;
      left: 9px;
      img {
        width: 14px;
        height: 14px;
      }
    }
    .keyinput {
      width: 186px;
      font-family:
        PingFang SC,
        PingFang SC;
      font-weight: 400;
      font-size: 12px;
      color: rgba(139, 157, 186, 0.8);
      line-height: 12px;
      text-align: left;
      font-style: normal;
      text-transform: none;
      ::v-deep .el-input__wrapper {
        background: rgba(210, 219, 246, 0.8) !important;
      }
    }
  }

  .aside-today-history {
    width: 100%;
    margin-bottom: 20px;
    min-height: 168px;
  }

  .aside-two-days-history {
    width: 100%;
    border-top: 1px solid rgba(139, 157, 186, 0.26);
    padding-top: 12px;
    margin-bottom: 20px;
    .items {
      height: 30px;
      margin-bottom: 12px;
      font-family:
        PingFang SC,
        PingFang SC;
      font-weight: 400;
      font-size: 12px;
      color: #424347;
      line-height: 12px;
      text-align: left;
      font-style: normal;
      text-transform: none;
    }
  }
}
.tips {
  font-family:
    PingFang SC,
    PingFang SC;
  font-weight: 400;
  font-size: 11px;
  color: #949eaf;
  line-height: 12px;
  text-align: left;
  font-style: normal;
  text-transform: none;
  margin-bottom: 15px;
}
.footer {
  position: relative;
  height: 60px;
  background: #ffffff;
  border-radius: 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;

  .footer-input {
    border-radius: 16px;
  }

  .footer-button {
    position: absolute;
    right: 1%;
  }

  :deep(.el-input__inner) {
    height: 60px;
  }
}
</style>
